<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-01-20
 * Time: 14:07
 */

namespace app\admin\controller;


use app\admin\model\CustomerModel;
use think\Session;

class Customer extends Base
{
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $user = new CustomerModel();
            $selectResult = $user->getCustomerByWhere($where, $offset, $limit);

            $status = config('customer_status');

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['thumbnail'] = '<img src="' . $vo['thumbnail'] . '" width="40px" height="40px">';
                $selectResult[$key]['status'] = $status[$vo['status']];

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $user->getAllCustomer($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }


    //添加客户
    public function customeradd()
    {
        $user_name = Session::get('username');
        if(request()->isPost()){

            $param = input('post.');

            $param['customer_people'] = $user_name;
            $param['status'] = 1;
            $param['create_time'] = date("Y-m-d h:i:sa");

            $user = new CustomerModel();
            $flag = $user->insertCustomer($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        return $this->fetch();
    }










    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '分配' => [
                'auth' => 'customer/distribution',
                'href' => url('customer/distribution', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '派单' => [
                'auth' => 'customer/paidan',
                'href' => url('customer/paidan', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '编辑' => [
                'auth' => 'customer/customeredit',
                'href' => url('customer/customeredit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'customer/customerdel',
                'href' => "javascript:customerDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}